﻿using System;
using System.Runtime.Serialization;

namespace MugenMvvmToolkit.Models
{
    /// <summary>
    ///     Provides data for the error changed event.
    /// </summary>
#if WPF
    [Serializable]
#endif
    [DataContract]
    public class AsyncValidationMessage : IEquatable<AsyncValidationMessage>
    {
        #region Equality members

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(AsyncValidationMessage other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Id.Equals(other.Id) && IsEndOperation.Equals(other.IsEndOperation);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((AsyncValidationMessage)obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            unchecked
            {
                return (Id.GetHashCode() * 397) ^ IsEndOperation.GetHashCode();
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncValidationMessage"/> class.
        /// </summary>
        public AsyncValidationMessage(Guid id, string propertyName, bool isEndOperation)
        {
            Id = id;
            IsEndOperation = isEndOperation;
            PropertyName = propertyName;
        }

        #endregion

        #region Properties

        /// <summary>
        ///     Gets the value that indicates that this operation is final.
        /// </summary>
        public bool IsEndOperation { get; private set; }

        /// <summary>
        ///     Gets the name of property, if any.
        /// </summary>
        public string PropertyName { get; private set; }

        /// <summary>
        ///     Gets the id of operation.
        /// </summary>
        public Guid Id { get; private set; }

        #endregion

        #region Methods

        /// <summary>
        /// Converts current message to an instance of <c>AsyncValidationMessage</c>.
        /// </summary>
        /// <returns>An instance of <c>AsyncValidationMessage</c>.</returns>
        public AsyncValidationMessage ToEndMessage()
        {
            return new AsyncValidationMessage(Id, PropertyName, true);
        }

        #endregion
    }
}